package com.sw.youvote.server;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import com.sw.youvote.server.persistentobjects.ImageDAO;
import com.sw.youvote.server.persistentobjects.ImageJdoDAO;
import com.sw.youvote.server.serverobjects.ImageBean;


public class FileServlet extends HttpServlet{ 
	
		public static final long serialVersionUID = 1;
		private ImageDAO imageDAO = new ImageJdoDAO();
		
	    public void doGet(HttpServletRequest request, HttpServletResponse response)  
	    throws ServletException, IOException { 
	       try{ 
	    	   
	    	   	String id = request.getParameter("id");
	    	   	ImageBean image = imageDAO.getImage(id);
	    	   	if ( image != null ) {
	    	   		response.setContentType("image/jpeg"); 
	    	   		response.getOutputStream().write(image.getImage()); 
	    	   		System.out.println("se ha servido la imagen " + id); 
	    	   	}else{
	    	   		System.out.println("imagen recuperada es null");
	    	   	}
	        } 
	        catch(Exception e){ 
	            e.printStackTrace(); 
	        }
	    } 
	} 

